//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using OpenLS.Vml.Serialization;

namespace OpenLS.Vml
{
    public class ShapeType : IOfficeXmlSerializable
    {
        public ShapeType()
        {
            AllCoreAttributes.CoreAttributes.Id = "_x0000_t" + Math.Abs(Guid.NewGuid().GetHashCode());
        }

        #region IOfficeXmlSerializable Members
        internal const string xmlName = "shapetype";
        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read(VmlConstants.VmlNamespace + xmlName))
            {
                ShapeTypeResolver r = c.GetData<ShapeTypeResolver>();
                r.AddShapeType(this);
                AllCoreAttributes.ReadXml(c);
                AllShapeAttributes.ReadXml(c);

                Adj = c.GetOptionalString("adj");
                Path = c.GetOptionalString("path");
                Master = c.GetOptionalString(VmlConstants.OfficeNamespace + "master");

                ShapeElement ele = ShapeElement.ReadShapeElement(c);
                while (ele != null)
                {
                    Elements.Add(ele);
                    ele = ShapeElement.ReadShapeElement(c);
                }
                c.WarnIf("complex");
            }
        }
       

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write (VmlConstants.VmlNamespace + xmlName))
            {
                    AllCoreAttributes.WriteXml(c);
                    AllShapeAttributes.WriteXml(c);

                c.SetOptionalString("adj", Adj);
                c.SetOptionalString("path", Path);
                c.SetOptionalString(VmlConstants.OfficeNamespace + "master", Master);
                foreach (ShapeElement e in Elements)
                    c.WriteElement(e);
                c.WarnIf("complex");
            }
        }
        #endregion
        private AllCoreAttributes _allCore;

        internal AllCoreAttributes AllCoreAttributes
        {
            get {
                if (_allCore == null)
                    _allCore = new AllCoreAttributes();
                return _allCore;
        }
        }

        private AllShapeAttributes _allShape;

        internal AllShapeAttributes AllShapeAttributes
        {
            get { 
                if (_allShape == null)
                    _allShape = new AllShapeAttributes();
                return _allShape;
        }
        }


        private string _marter;

        public string Master
        {
            get { return _marter; }
            set { _marter = value; }
        }

        private string _adj;

        public string Adj
        {
            get { return _adj; }
            set { _adj = value; }
        }
        private string _path;

        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        private readonly List<ShapeElement> _elements = new List<ShapeElement>();

        public List<ShapeElement> Elements
        {
            get { return _elements; }
        }


        internal string Id
        {
            get
            {
                return AllCoreAttributes.CoreAttributes.Id;
            }
        }

        public string CoordinateSize
        {
            get { return AllCoreAttributes.CoreAttributes.CoordSize; }
            set { AllCoreAttributes.CoreAttributes.CoordSize = value; }
        }
	
    }
}
